/********************************************
 * Copyright (C) 2019-2020 Intel Corporation
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 ********************************************/
#ifndef _NNP_IPC_C2H_EVENTS_H
#define _NNP_IPC_C2H_EVENTS_H

/**
 * The following describes the possible values for a c2h_event_report message
 * sent from card to host to report on some error or other events.
 *
 * The c2h_event_report message has the following fields available to describe
 * the event:
 *    event_code  - 8 bits value describing the type of event
 *    event_val   - 8 bits value - interpetation depends on event_code
 *    context_id  - the protocol_id of the context in which the event was occurred.
 *    obj_id      - 16 bits, interpretation depends on event_code, usually used
 *                 to hold an inference object (devre, network, ...) protocol ID.
 *    obj_id_2    - 16 bits, in case obj_id is not enough to describe the object
 * In this file we define the possible values for the above fields and document
 * each field meaning for each possible event_code.
 */

/**
 * Event codes ranges
 *
 * Those ranges should match the definition of RuntimeEventCodes
 * defined in include/nnpcsInference.h and used by the daemon/runtime
 * interface !!!
 *
 * error codes are grouped into the following ranges:
 *     0 -   3   ==> non error events generated by daemon/runtime
 *     4 -  47   ==> non error events generated by card kernel driver
 *    48 -  51   ==> non-critical error events generated by daemon/runtime
 *    52 -  95   ==> non-critical error events generatd by kernel driver
 *    96 - 103   ==> context-critical error events generated by daemon/runtime
 *   104 - 111   ==> context-critical error events generated by kernel driver
 *   112 - 119   ==> card-critical error events generated by daemon/runtime
 *   120 - 127   ==> card-critical error events generated by kernel driver
 *
 * context-critical error event is one that puts the infer context in an
 * un-recovarable error state.
 * card-critical error event is one that make the card not useful for inference
 * request until it is reset.
 */
#define EVENT_NON_ERR_START             0
#define EVENT_NON_ERR_DRV_START         4
#define EVENT_ERR_START                48
#define EVENT_ERR_DRV_START            52
#define EVENT_CONTEXT_FATAL_START      96
#define EVENT_CONTEXT_FATAL_DRV_START 104
#define EVENT_CARD_FATAL_START        112
#define EVENT_CARD_FATAL_DRV_START    120

#define is_context_fatal_event(e)  ((e) >= EVENT_CONTEXT_FATAL_START && (e) < EVENT_CARD_FATAL_START)
#define is_card_fatal_event(e)     ((e) >= EVENT_CARD_FATAL_START)
#define is_card_fatal_drv_event(e)     ((e) >= EVENT_CARD_FATAL_DRV_START)

#define NNP_IPC_RUNTIME_DONE   (EVENT_NON_ERR_START + 1)
/*            MAX offset for EVENT_NON_ERR_START is 3 */

/* non-error event codes */
#define NNP_IPC_CREATE_CONTEXT_SUCCESS   (EVENT_NON_ERR_DRV_START + 0)
#define NNP_IPC_CREATE_DEVRES_SUCCESS    (EVENT_NON_ERR_DRV_START + 1)
#define NNP_IPC_CREATE_COPY_SUCCESS      (EVENT_NON_ERR_DRV_START + 2)
#define NNP_IPC_EXECUTE_COPY_SUCCESS     (EVENT_NON_ERR_DRV_START + 3)
#define NNP_IPC_DEVRES_DESTROYED         (EVENT_NON_ERR_DRV_START + 4)
#define NNP_IPC_COPY_DESTROYED           (EVENT_NON_ERR_DRV_START + 5)
#define NNP_IPC_CONTEXT_DESTROYED        (EVENT_NON_ERR_DRV_START + 6)
#define NNP_IPC_CREATE_DEVNET_SUCCESS    (EVENT_NON_ERR_DRV_START + 7)
#define NNP_IPC_DEVNET_DESTROYED         (EVENT_NON_ERR_DRV_START + 8)
#define NNP_IPC_CREATE_INFREQ_SUCCESS    (EVENT_NON_ERR_DRV_START + 9)
#define NNP_IPC_INFREQ_DESTROYED         (EVENT_NON_ERR_DRV_START + 10)
#define NNP_IPC_RECOVER_CONTEXT_SUCCESS  (EVENT_NON_ERR_DRV_START + 11)
#define NNP_IPC_THERMAL_TRIP_EVENT       (EVENT_NON_ERR_DRV_START + 12)
#define NNP_IPC_DEVNET_ADD_RES_SUCCESS   (EVENT_NON_ERR_DRV_START + 13)
#define NNP_IPC_DEVICE_STATE_CHANGED     (EVENT_NON_ERR_DRV_START + 14)
#define NNP_IPC_DEVNET_RESOURCES_RESERVATION_SUCCESS  (EVENT_NON_ERR_DRV_START + 15)
#define NNP_IPC_DEVNET_RESOURCES_RELEASE_SUCCESS  (EVENT_NON_ERR_DRV_START + 16)
#define NNP_IPC_CREATE_CHANNEL_SUCCESS   (EVENT_NON_ERR_DRV_START + 17)
#define NNP_IPC_CHANNEL_DESTROYED        (EVENT_NON_ERR_DRV_START + 18)
#define NNP_IPC_CHANNEL_SET_RB_SUCCESS   (EVENT_NON_ERR_DRV_START + 19)
#define NNP_IPC_CHANNEL_MAP_HOSTRES_SUCCESS   (EVENT_NON_ERR_DRV_START + 20)
#define NNP_IPC_CHANNEL_UNMAP_HOSTRES_SUCCESS (EVENT_NON_ERR_DRV_START + 21)
#define NNP_IPC_ABORT_REQUEST            (EVENT_NON_ERR_DRV_START + 22)
#define NNP_IPC_GET_FIFO                 (EVENT_NON_ERR_DRV_START + 23)
#define NNP_IPC_CREATE_CMD_SUCCESS       (EVENT_NON_ERR_DRV_START + 24)
#define NNP_IPC_CMD_DESTROYED            (EVENT_NON_ERR_DRV_START + 25)
#define NNP_IPC_EXECUTE_CMD_COMPLETE     (EVENT_NON_ERR_DRV_START + 26)
#define NNP_IPC_DEVNET_SET_PROPERTY_SUCCESS  (EVENT_NON_ERR_DRV_START + 27)
#define NNP_IPC_EXECUTE_CPYLST_SUCCESS   (EVENT_NON_ERR_DRV_START + 28)
#define NNP_IPC_GET_CR_FIFO_REPLY        (EVENT_NON_ERR_DRV_START + 29)
#define NNP_IPC_P2P_PEERS_CONNECTED      (EVENT_NON_ERR_DRV_START + 30)
#define NNP_IPC_P2P_PEER_DEV_UPDATED     (EVENT_NON_ERR_DRV_START + 31)
#define NNP_IPC_EXECUTE_COPY_SUBRES_SUCCESS  (EVENT_NON_ERR_DRV_START + 32)
/*                   MAX offset for EVENT_NON_ERR_DRV_START is 43 */

/* non-critical error event codes */
#define NNP_IPC_CREATE_CONTEXT_FAILED    (EVENT_ERR_DRV_START + 0)
#define NNP_IPC_CREATE_DEVRES_FAILED     (EVENT_ERR_DRV_START + 1)
#define NNP_IPC_CREATE_COPY_FAILED       (EVENT_ERR_DRV_START + 2)
#define NNP_IPC_DESTROY_CONTEXT_FAILED   (EVENT_ERR_DRV_START + 3)
#define NNP_IPC_DESTROY_DEVRES_FAILED    (EVENT_ERR_DRV_START + 4)
#define NNP_IPC_DESTROY_COPY_FAILED      (EVENT_ERR_DRV_START + 5)
#define NNP_IPC_CREATE_SYNC_FAILED       (EVENT_ERR_DRV_START + 6)
#define NNP_IPC_ERROR_SUB_RESOURCE_LOAD_FAILED      (EVENT_ERR_DRV_START + 7)
#define NNP_IPC_CREATE_DEVNET_FAILED     (EVENT_ERR_DRV_START + 8)
#define NNP_IPC_DESTROY_DEVNET_FAILED    (EVENT_ERR_DRV_START + 9)
#define NNP_IPC_CREATE_INFREQ_FAILED     (EVENT_ERR_DRV_START + 10)
#define NNP_IPC_DESTROY_INFREQ_FAILED    (EVENT_ERR_DRV_START + 11)
#define NNP_IPC_RECOVER_CONTEXT_FAILED   (EVENT_ERR_DRV_START + 12)
#define NNP_IPC_ERROR_MCE_CORRECTABLE    (EVENT_ERR_DRV_START + 13)
#define NNP_IPC_ERROR_MCE_UNCORRECTABLE  (EVENT_ERR_DRV_START + 14)
#define NNP_IPC_DEVNET_ADD_RES_FAILED    (EVENT_ERR_DRV_START + 15)
#define NNP_IPC_DEVNET_RESOURCES_RESERVATION_FAILED (EVENT_ERR_DRV_START + 16)
#define NNP_IPC_DEVNET_RESOURCES_RELEASE_FAILED     (EVENT_ERR_DRV_START + 17)
#define NNP_IPC_CREATE_CHANNEL_FAILED    (EVENT_ERR_DRV_START + 18)
#define NNP_IPC_DESTROY_CHANNEL_FAILED   (EVENT_ERR_DRV_START + 19)
#define NNP_IPC_CHANNEL_SET_RB_FAILED    (EVENT_ERR_DRV_START + 20)
#define NNP_IPC_CREATE_CMD_FAILED        (EVENT_ERR_DRV_START + 21)
#define NNP_IPC_DESTROY_CMD_FAILED       (EVENT_ERR_DRV_START + 22)
#define NNP_IPC_CHANNEL_MAP_HOSTRES_FAILED   (EVENT_ERR_DRV_START + 23)
#define NNP_IPC_CHANNEL_UNMAP_HOSTRES_FAILED (EVENT_ERR_DRV_START + 24)
#define NNP_IPC_DEVNET_SET_PROPERTY_FAILED  (EVENT_ERR_DRV_START + 25)
#define NNP_IPC_ERROR_DRAM_ECC_CORRECTABLE (EVENT_ERR_DRV_START + 26)
#define NNP_IPC_EXECUTE_COPY_FAILED        (EVENT_ERR_DRV_START + 27)
#define NNP_IPC_SCHEDULE_INFREQ_FAILED     (EVENT_ERR_DRV_START + 28)
#define NNP_IPC_EXECUTE_CPYLST_FAILED      (EVENT_ERR_DRV_START + 29)
#define NNP_IPC_EXECUTE_COPY_SUBRES_FAILED  (EVENT_ERR_DRV_START + 30)
#define NNP_IPC_EC_FAILED_TO_RELEASE_CREDIT  (EVENT_ERR_DRV_START + 31)
#define NNP_IPC_DMA_HANG_DETECTED            (EVENT_ERR_DRV_START + 32)
/*                   MAX offset for EVENT_ERR_DRV_START is 43 */

/* context critical error event codes */
#define NNP_IPC_ERROR_RUNTIME_LAUNCH     (EVENT_CONTEXT_FATAL_START + 0)
#define NNP_IPC_ERROR_RUNTIME_DIED       (EVENT_CONTEXT_FATAL_START + 1)
/*                   MAX offset for EVENT_CONTEXT_FATAL_START is 7 */

#define NNP_IPC_CONTEXT_EXEC_ERROR            (EVENT_CONTEXT_FATAL_DRV_START + 0)
#define NNP_IPC_CTX_DRAM_ECC_UNCORRECTABLE    (EVENT_CONTEXT_FATAL_DRV_START + 1)
/*                   MAX offset for EVENT_CONTEXT_FATAL_DRV_START is 7 */

/* card critical error event codes */
#define NNP_IPC_ERROR_OS_CRASHED          (EVENT_CARD_FATAL_START + 0)
#define NNP_IPC_ERROR_DRAM_ECC_UNCORRECTABLE_FATAL  (EVENT_CARD_FATAL_START + 1)
#define NNP_IPC_ERROR_FATAL_ICE_ERROR     (EVENT_CARD_FATAL_START + 2)
/*                   MAX offset for EVENT_CARD_FATAL_START is 7 */

/* card critical and driver fatal*/
#define NNP_IPC_ERROR_PCI_ERROR           (EVENT_CARD_FATAL_DRV_START + 0)
#define NNP_IPC_ERROR_MCE_UNCORRECTABLE_FATAL  (EVENT_CARD_FATAL_DRV_START + 1)
#define NNP_IPC_ERROR_CARD_RESET          (EVENT_CARD_FATAL_DRV_START + 2)
#define NNP_IPC_ERROR_CHANNEL_KILLED      (EVENT_CARD_FATAL_DRV_START + 3)
#define NNP_IPC_ERROR_PROTOCOL_ERROR      (EVENT_CARD_FATAL_DRV_START + 4)
#define NNP_IPC_FATAL_DMA_HANG_DETECTED   (EVENT_CARD_FATAL_DRV_START + 5)
/*                   MAX offset for EVENT_CARD_FATAL_DRV_START is 7 */


enum event_val {
	NNP_IPC_NO_ERROR		= 0,
	NNP_IPC_NO_SUCH_CONTEXT		= 1,
	NNP_IPC_NO_SUCH_DEVRES		= 2,
	NNP_IPC_NO_SUCH_COPY		= 3,
	NNP_IPC_NO_SUCH_NET		= 4,
	NNP_IPC_NO_SUCH_INFREQ		= 5,
	NNP_IPC_ALREADY_EXIST		= 6,
	NNP_IPC_NO_DAEMON		= 7,
	NNP_IPC_NO_MEMORY		= 8,
	NNP_IPC_RUNTIME_FAILED		= 9,
	NNP_IPC_RUNTIME_LAUNCH_FAILED	= 10,
	NNP_IPC_DMA_ERROR		= 11,
	NNP_IPC_RUNTIME_NOT_SUPPORTED	= 12,
	NNP_IPC_RUNTIME_INVALID_EXECUTABLE_NETWORK_BINARY = 13,
	NNP_IPC_RUNTIME_INFER_MISSING_RESOURCE        = 14,
	NNP_IPC_RUNTIME_INFER_EXEC_ERROR              = 15,
	NNP_IPC_RUNTIME_INFER_SCHEDULE_ERROR          = 16,
	NNP_IPC_CONTEXT_BROKEN                        = 17,
	NNP_IPC_DEVNET_RESERVE_INSUFFICIENT_RESOURCES = 18,
	NNP_IPC_TIMEOUT_EXCEEDED        = 19,
	NNP_IPC_ECC_ALLOC_FAILED        = 20,
	NNP_IPC_NO_SUCH_CHANNEL         = 21,
	NNP_IPC_NO_SUCH_CMD             = 22,
	NNP_IPC_NO_SUCH_HOSTRES         = 23,
	NNP_IPC_DEVNET_EDIT_BUSY        = 24,
	NNP_IPC_DEVNET_EDIT_ERROR       = 25,
	NNP_IPC_NOT_SUPPORTED           = 26,
	NNP_IPC_ICEDRV_INFER_EXEC_ERROR = 27,
	NNP_IPC_ICEDRV_INFER_EXEC_ERROR_NEED_RESET = 28,
	NNP_IPC_ICEDRV_INFER_EXEC_ERROR_NEED_CARD_RESET = 29,
	NNP_IPC_NO_EXEC_ERRORS          = 30,
	NNP_IPC_IO_ERROR                = 31,
	NNP_IPC_INPUT_IS_DIRTY          = 32,
	NNP_IPC_FAILED_TO_RELEASE_CREDIT = 33,

	//Non failure events
	NNP_IPC_CMDLIST_FINISHED       = 128,
};

int event_valToNNPErrno(enum event_val event_val);

/**
 *
 * event_code                        event_val         ContextID   obj_id                         obj_id_2
 * ---------                        --------         ---------   ------                        ------
 * NNP_IPC_CREATE_CONTEXT_SUCCESS   0                Valid       Not-Valid                     Not-Valid
 * NNP_IPC_CREATE_DEVRES_SUCCESS    bid (for p2p     Valid       Device resource protocol_id    offset (valid for p2p resource only)
 * NNP_IPC_CREATE_COPY_SUCCESS      0                Valid       Copy handle protocol_id        Not-Valid
 * NNP_IPC_CREATE_DEVNET_SUCCESS    0                Valid       Device network protocol_id     Not-Valid
 * NNP_IPC_CREATE_INFREQ_SUCCESS    devnetID         Valid       inf req protocol_id            Not-Valid
 * NNP_IPC_EXECUTE_COPY_SUCCESS     0                Valid       Copy handle protocol_id        Not-Valid or Cmd list protocol_id(in case Cmd list complete)
 * NNP_IPC_EXECUTE_COPY_SUBRES_SUCCESS   0           Valid       Copy handle protocol_id        Not-Valid or Cmd list protocol_id(in case Cmd list complete)
 * NNP_IPC_CREATE_CHANNEL_SUCCESS   0                Not-Valid   Channel protocol_id            Not-Valid
 * NNP_IPC_CHANNEL_SET_RB_SUCCESS   0                Not-Valid   Channel protocol_id            rb_id
 * NNP_IPC_CHANNEL_MAP_HOSTRES_SUCCESS   0           Not-Valid   Channel protocol_id            Hostres id
 * NNP_IPC_CHANNEL_UNMAP_HOSTRES_SUCCESS 0           Not-Valid   Channel protocol_id            Hostres id
 * NNP_IPC_DEVRES_DESTROYED         0                Valid       Device resource protocol_id    Not-Valid
 * NNP_IPC_COPY_DESTROYED           0                Valid       Copy handle protocol_id        Not-Valid
 * NNP_IPC_CONTEXT_DESTROYED        0                Valid       Not-Valid                     Not-Valid
 * NNP_IPC_DEVNET_DESTROYED         0                Valid       Device network protocol_id     Not-Valid
 * NNP_IPC_INFREQ_DESTROYED         0                Valid       inf req protocol_id            devnetID
 * NNP_IPC_CHANNEL_DESTROYED        0                Not-valid   channel protocol_id            Not-Valid
 * NNP_IPC_CHANNEL_SET_RB_FAILED    0                Not-valid   channel protocol_id            rb_id
 * NNP_IPC_CREATE_CMD_SUCCESS       0                Valid       Cmd list protocol_id           Not-Valid
 * NNP_IPC_CMD_DESTROYED            0                Valid       Cmd list protocol_id           Not-Valid
 * NNP_IPC_EXECUTE_CMD_COMPLETE     0                Valid       Cmd list protocol_id           Not-Valid
 *
 * NNP_IPC_EXECUTE_CPYLST_SUCCESS   0 or
 *                                  enum event_val
 *                                 (NNP_IPC_EXECUTE_CPYLST_SUCCESS)
 *                                                   Valid  Cmd list protocol_id           Copy index in Cmd list
 *
 * NNP_IPC_CREATE_CONTEXT_FAILED    enum event_val   Valid       Not-Valid                     Not-Valid
 * NNP_IPC_CREATE_DEVRES_FAILED     enum event_val   Valid       Device resource protocol_id    Not-Valid
 * NNP_IPC_CREATE_COPY_FAILED       enum event_val   Valid       Copy handle protocol_id        Not-Valid
 * NNP_IPC_CREATE_DEVNET_FAILED     enum event_val   Valid       Device network protocol_id     Not-Valid
 * NNP_IPC_CREATE_INFREQ_FAILED     enum event_val   Valid       inf req protocol_id            devnetID
 * NNP_IPC_DESTROY_CONTEXT_FAILED   enum event_val   Valid       Not-Valid                     Not-Valid
 * NNP_IPC_DESTROY_DEVRES_FAILED    enum event_val   Valid       Device resource protocol_id    Not-Valid
 * NNP_IPC_DESTROY_COPY_FAILED      enum event_val   Valid       Copy handle protocol_id        Not-Valid
 * NNP_IPC_DESTROY_DEVNET_FAILED    enum event_val   Valid       Device network protocol_id     Not-Valid
 * NNP_IPC_DESTROY_INFREQ_FAILED    enum event_val   Valid       inf req protocol_id            devnetID
 * NNP_IPC_EXECUTE_COPY_FAILED      enum event_val   Valid       Copy handle protocol_id        Not-Valid or Cmd list protocol_id
 * NNP_IPC_EXECUTE_COPY_SUBRES_FAILED enum event_val Valid       Copy handle protocol_id        Not-Valid or Cmd list protocol_id
 * NNP_IPC_SCHEDULE_INFREQ_FAILED   enum event_val   Valid       inf req protocol_id            devnetID
 * NNP_IPC_CREATE_SYNC_FAILED       enum event_val   Valid       syncSeq value                 Not-Valid
 * NNP_IPC_CREATE_CHANNEL_FAILED    enum event_val   Not-Valid   Channel protocol_id            Not-Valid
 * NNP_IPC_DESTROY_CHANNEL_FAILED   enum event_val   Not-Valid   Channel protocol_id            Not-Valid
 * NNP_IPC_CHANNEL_MAP_HOSTRES_FAILED   enum         Not-Valid   Channel protocol_id            Hostres id
 * NNP_IPC_CHANNEL_UNMAP_HOSTRES_FAILED enum         Not-Valid   Channel protocol_id            Hostres id
 * NNP_IPC_ERROR_RUNTIME_LAUNCH     0                Valid       Not-Valid                     Not-Valid
 * NNP_IPC_ERROR_RUNTIME_DIED       0                Valid       Not-Valid                     Not-Valid
 * NNP_IPC_ERROR_OS_CRASHED         0                Not-Valid   dump_size (low 16bits)        dump_size (high 16bits)
 * NNP_IPC_ERROR_PCI_ERROR          error_type (1)   Not-Valid   Not-Valid                     Not-Valid
 * NNP_IPC_ERROR_CARD_RESET         0 (2)            Not-Valid   Not-Valid                     Not-Valid
 * NNP_IPC_THERMAL_TRIP_EVENT       trip_point_num   Not-Valid   trip temperature              event temperature
 * NNP_IPC_DEVICE_STATE_CHANGED     0 (3)            DevState[0-15] DevState[16-31]            Not-Valid
 * NNP_IPC_GET_FIFO                 tr_id            NotValid    base address offset in pages  Not-Valid
 * NNP_IPC_CREATE_CMD_FAILED        enum event_val   Valid       Cmd list protocol_id           Not-Valid
 * NNP_IPC_DESTROY_CMD_FAILED       enum event_val   Valid       Cmd list protocol_id           Not-Valid
 * NNP_IPC_EXECUTE_CPYLST_FAILED    enum event_val   Valid       Cmd list protocol_id           Copy index in Cmd list
 * NNP_IPC_ABORT_REQUEST            0 (4)            Not-Valid   Not-Valid                     Not-Valid
 * NNP_IPC_ERROR_FATAL_ICE_ERROR    Failed context_id No-Valid    Not-Valid                     NoT-Valid
 * NNP_IPC_CONTEXT_EXEC_ERROR       Failed cmd-type  Valid       cmd obj protocol_id            devnetID
 * NNP_IPC_GET_CR_FIFO              0                ChanId      dev_id                        base address offset in pages
 *
 * Comments:
 *    (1) - this event is generated by the host. error_type is value passed to
 *    pci_error_detected callback, see src/driver/host/nnpdrv_pcie.h for
 *    possible values.
 *
 *    (2) - this event is generated by the host when a card reset is forced.
 *    (3) - host generated event when device state changes
 *    (4) - host generated event when "nnpi_ctl disable -abort" is requested
 */
#endif
